package com.wing.purchase.controller.admin;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.enums.ProductEnum;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.purchase.model.response.PurQualityTestingTaskDetailSearchVo;
import com.wing.purchase.service.PurQualityTestingTaskDetailService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.1.0
 * @Author hz
 * @Date 2024-11-16 11:05
 **/
@Api(value = "Admin-采购-质检任务明细", tags = "#/purchase/purQuality/purQualityTaskDetail")
@RestController("purQualityTestingTaskDetailController")
@RequestMapping("purchase/admin/purQualityTaskDetail")
@RequiredArgsConstructor
@Slf4j
public class PurQualityTestingTaskDetailController {

    private final PurQualityTestingTaskDetailService purQualityTestingTaskDetailService;

    @ApiOperation(value = "查询采购质检任务明细", notes = "搜索-质检明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<PurQualityTestingTaskDetailSearchVo> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String sourceType
            ,String supplierId
            ,String code
            ,String orderCode
            ,String qualityTestingCategoryId
            ,String startTime
            ,String endTime
    ){

        Page<PurQualityTestingTaskDetailSearchVo> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        params.put("sourceType", sourceType);
        params.put("supplierId", supplierId);
        params.put("code", code);
        params.put("orderCode", orderCode);
        params.put("qualityTestingCategoryId", qualityTestingCategoryId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);

        IPage<PurQualityTestingTaskDetailSearchVo> list = purQualityTestingTaskDetailService.pageList4app(pg,params);
        List<PurQualityTestingTaskDetailSearchVo> records = list.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            for (PurQualityTestingTaskDetailSearchVo record : records) {
                record.setSourceTypeName(ProductEnum.purSourceTypeEmum.getNameByCode(record.getSourceType()));
                record.setStatusName(MaintenanceEnum.QualityStatus.getNameByCode(Integer.parseInt(record.getStatus())));
            }
        }
        return new PageResult<>(list.getRecords(), list.getTotal());
    }




    /**
     * 查询采购质检详情
     * @param recordId 记录id
     * @return
     */
    @ApiOperation(value = "采购质检明细详情", notes = "采购-明细详情")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/detail/{recordId}")
    public JsonResult<PurQualityTestingTaskDetailSearchVo> detail(@PathVariable("recordId") String recordId) {
        PurQualityTestingTaskDetailSearchVo purQualityTestingTaskDetailVo = purQualityTestingTaskDetailService.detail(recordId);
        return JsonResult.success(purQualityTestingTaskDetailVo);
    }
}
